/*

文件名称:POIMap.js
文件描述:高德地图调用
设计作者:王敏
编码作者:王敏
完成日期:2015-8

调用描述:外部只需要调用keywordSearch方法进行初始化

*/

//关键字查询 
  var mark=new Array();
  var windowsArr = new Array();
function keywordSearch(keywords,city){ 
  mark=[];
  map.clearMap();
    //构造关键字查询实例 
  
    var PoiSearchOption = { 
  
        srctype:"POI",//数据来源 
  
        type:"",//数据类别 
  
        number:10,//每页数量,默认10 
  
        batch:1,//请求页数，默认1 
  
        range:3000, //查询范围，默认3000米 
  
        ext:""//扩展字段 
  
    }; 
  
    var poiSearch = new MMap.PoiSearch(PoiSearchOption); 
  
    poiSearch.byKeywords(keywords,city,poiSearch_CallBack); 
  
} 
  
//关键字查询返回结果显示 
  
var markers = new Array(),markerLngLat = new Array(),infos = new Array(); 
  
function poiSearch_CallBack(data){ 
  
    map.clearOverlaysByType('marker'); 
  
    map.clearInfoWindow(); 
  
    if(data.status === 'E0'){ 
  
        var resultStr = "";markers=[];infos=[];markerLngLat=[]; 
        if(data.list.length<=0){
            confirm("没有查询到相关地点!");
            return false;
        }
        for(var i = 0;i < data.list.length;i++){ 
         resultStr += "<div id='divid" + (i + 1) + "' onmouseover='openMarkerTipById(" + i + ",this)' onmouseout='onmouseout_MarkerStyle(" + (i + 1) + ",this)' style=\"font-size: 12px;cursor:pointer;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\"><table><tr><td><img src=\"http://webapi.amap.com/images/" + (i + 1) + ".png\"></td>" + "<td><h3><font color=\"#00a6ac\">名称: " + data.list[i].name + "</font></h3>";
            resultStr += TipContents(data.list[i].type, data.list[i].address, data.list[i].tel) + "</td></tr></table></div>";
            addMarkerTip(i,data.list[i]); 
        } 
  
        map.setFitView(); 
  
    }else if(data.status === "E1"){ 
  
         confirm("未查找到任何结果!<br />建议：<br />1.请确保所有字词拼写正确。<br />2.尝试不同的关键字。<br />3.尝试更宽泛的关键字。");    
  
    }else{ 
  
         confirm("错误信息："+data.status+"请对照API Server v2.0.0 简明提示码对照表查找错误类型"); 
  
    } 
  
  
} 
 

    function addMarkerTip(i, d) {
        var markerOption = {
            map: map,
            icon: "http://webapi.amap.com/images/" + (i + 1) + ".png",
            position: new MMap.LngLat(d.x, d.y),
            topWhenMouseOver: true

        };
        var mar = new MMap.Marker(markerOption);
        mark.push(new MMap.LngLat(d.x, d.y));
        map.addOverlays(mar);
        // var infoWindow = new MMap.InfoWindow({
        //     content: "<h3><font color=\"#00a6ac\">  " + (i + 1) + ". " + d.name + "</font></h3>" + TipContents(d.type, d.address, d.tel, d.x, d.y),
        //     size:new MMap.Size(300, 0), 
        //     autoMove: true,
        //     offset:new MMap.Pixel(-90,-55)   
        // });
var content1="<div style='background-color:#FFF;width:300px;padding:0px 10px'><h3><font color=\"#00a6ac\">  " + (i + 1) + ". " + d.name + "</font></h3>" + TipContents(d.type, d.address, d.tel, d.x, d.y)+"</div>";
var infoWindow=InfoWindow(null,content1,300,d.x,d.y,10,-145,true);
        windowsArr.push(infoWindow.winD);
        var aa = function(e) {
            infoWindow.winD.open(map, mar.getPosition());
        };
        map.bind(mar, "mouseover", aa);
    }
    //回调函数
    function keywordSearch_CallBack(data) {
        map.clearMap();
        var resultStr = "";
        var poiArr = data.poiList.pois;
        var resultCount = poiArr.length;

        if (resultCount <= 0) {
            $.messager.alert("提示信息", "没有查询到相关地点！", "question", function() {
                return false;
            });
        }

        for (var i = 0; i < resultCount; i++) {
            resultStr += "<div id='divid" + (i + 1) + "' onmouseover='openMarkerTipById1(" + i + ",this)' onmouseout='onmouseout_MarkerStyle(" + (i + 1) + ",this)' style=\"font-size: 12px;cursor:pointer;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\"><table><tr><td><img src=\"http://webapi.amap.com/images/" + (i + 1) + ".png\"></td>" + "<td><h3><font color=\"#00a6ac\">名称: " + data.list[i].name + "</font></h3>";
            resultStr += TipContents(data.list[i].type, data.list[i].address, data.list[i].tel) + "</td></tr></table></div>";
            addMarker(i, poiArr[i]);
        }
        map.setFitView();
    }

    function TipContents(type, address, tel, lng, lat) { //窗体内容

        type = "暂无";

        if (address === "" || address === "undefined" || address === null || address === " undefined" || typeof address === "undefined") {
            address = "暂无";
        }
        if (tel === "" || tel === "undefined" || tel === null || tel === " undefined" || typeof address === "tel") {
            tel = "暂无";
        }
        var str = "<div style='text-overflow:ellipsis;white-space:nowrap;overflow:hidden;width:290px;height:20px'>地址：" + address + "</div>  电话：" + tel + " <br />  类型：" + type;

        str += "<br /> <button type=\"button\" class=\"btn btn-success btn-sm\" onclick=\"getPos(" + lng + "," + lat + ",'" + address + "');\">确定</button> ";

        return str;
    }

    function openMarkerTipById1(pointid, thiss) { //根据id 打开搜索结果点tip
        thiss.style.background = '#CAE1FF';
        windowsArr[pointid].open(map, mark[pointid]);
    }

    function onmouseout_MarkerStyle(pointid, thiss) { //鼠标移开后点样式恢复
        thiss.style.background = "";
    }